package com.yixuan.mt.common.service.impl;

import com.yixuan.mt.common.local.buckets.BucketsMeta;
import com.yixuan.mt.common.constant.PathConstant;
import com.yixuan.mt.common.entity.Objects;
import com.yixuan.mt.common.local.objects.ObjectsMeta;
import com.yixuan.mt.common.request.UploadRequest;
import com.yixuan.mt.common.service.ObjectsService;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ObjectsServiceImpl implements ObjectsService {
    @Override
    public List<Objects> getObjects(Integer lastId, String parentPath) throws IOException {
        int objectNumber = BucketsMeta.getIntInLocalBucketMeta(parentPath, "objectNumber");
        if (objectNumber == 0) {
            return null;
        }
        String[] objectPathList = BucketsMeta.getObjectPathListInLocalBucketMeta(parentPath.substring(1), 0, objectNumber);
        List<Objects> objectBasicInfoList = new ArrayList<>();
        for (String objectPath : objectPathList) {
            Objects objectBasicInfo = ObjectsMeta.getBasicInfoFromObjectsMeta(objectPath);
            objectBasicInfoList.add(objectBasicInfo);
        }
        return objectBasicInfoList;
    }

    @Override
    public void postObjects(String objectPath, File file) {
    }

    @Override
    public void upload(UploadRequest uploadRequest) throws IOException {
        String objectPath = uploadRequest.getObjectPath();
        MultipartFile object = uploadRequest.getObjectFile();

        String fullPath = new File(PathConstant.FILE_RESOURCE_PATH_PREFIX).getAbsolutePath() + objectPath +  objectPath.substring(objectPath.lastIndexOf("/"));
        File file = new File(fullPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        object.transferTo(file);
        // 桶名
        String bucketName = objectPath.substring(0, objectPath.indexOf("/", 1));
        // 更新对象数量
        BucketsMeta.updateObjectNumberInLocalBucketMeta(bucketName);
        // 添加对象路径到桶元数据文件
        BucketsMeta.addObjectToLocalBucketMeta(bucketName, objectPath);
        // 创建对象元数据
        ObjectsMeta.createObjectsMeta(objectPath, file);
    }

    @Override
    public void postFolder(String folderPath) {

    }

    @Override
    public void deleteObjects(String name, List<Integer> ids) {

    }

    @Override
    public ResponseEntity<InputStreamResource> download(String objectPath) throws FileNotFoundException {
        FileInputStream fi = new FileInputStream(PathConstant.FILE_RESOURCE_PATH_PREFIX + objectPath + objectPath.substring(objectPath.lastIndexOf("/")));
        InputStreamResource isr = new InputStreamResource(fi);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-Disposition", "attachment;filename=" + objectPath.substring(objectPath.lastIndexOf("/") + 1))
                .body(isr);
    }

    @Override
    public Boolean isParentPathExist(String parentPath) {
        return null;
    }
}
